package cn.iocoder.yudao.module.worker.dal.mysql.clerk;

import java.util.*;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX;
import cn.iocoder.yudao.module.worker.controller.app.clerk.vo.AppClerkPageReqVO;
import cn.iocoder.yudao.module.worker.dal.dataobject.clerk.ClerkDO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Mapper;
import cn.iocoder.yudao.module.worker.controller.admin.clerk.vo.*;

/**
 * 店员信息 Mapper
 *
 * @author Jerry
 */
@Mapper
public interface ClerkMapper extends BaseMapperX<ClerkDO> {

    default PageResult<ClerkDO> selectPage(ClerkPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<ClerkDO>()
                .eqIfPresent(ClerkDO::getUserId, reqVO.getUserId())
                .likeIfPresent(ClerkDO::getNickname, reqVO.getNickname())
                .eqIfPresent(ClerkDO::getAvatar, reqVO.getAvatar())
                .eqIfPresent(ClerkDO::getSex, reqVO.getSex())
                .eqIfPresent(ClerkDO::getAge, reqVO.getAge())
                .eqIfPresent(ClerkDO::getWeixin, reqVO.getWeixin())
                .eqIfPresent(ClerkDO::getMobile, reqVO.getMobile())
                .eqIfPresent(ClerkDO::getCity, reqVO.getCity())
                .eqIfPresent(ClerkDO::getExperience, reqVO.getExperience())
                .eqIfPresent(ClerkDO::getSound, reqVO.getSound())
                .eqIfPresent(ClerkDO::getSoundType, reqVO.getSoundType())
                .betweenIfPresent(ClerkDO::getSoundTime, reqVO.getSoundTime())
                .eqIfPresent(ClerkDO::getAlbums, reqVO.getAlbums())
                .eqIfPresent(ClerkDO::getIntro, reqVO.getIntro())
                .betweenIfPresent(ClerkDO::getOnlineTime, reqVO.getOnlineTime())
                .eqIfPresent(ClerkDO::getOnlineStatus, reqVO.getOnlineStatus())
                .eqIfPresent(ClerkDO::getCatIds, reqVO.getCatIds())
                .eqIfPresent(ClerkDO::getCatList, reqVO.getCatList())
                .eqIfPresent(ClerkDO::getGameIds, reqVO.getGameIds())
                .eqIfPresent(ClerkDO::getGameList, reqVO.getGameList())
                .eqIfPresent(ClerkDO::getGoodsIds, reqVO.getGoodsIds())
                .eqIfPresent(ClerkDO::getFans, reqVO.getFans())
                .eqIfPresent(ClerkDO::getOrderNum, reqVO.getOrderNum())
                .eqIfPresent(ClerkDO::getBlindStatus, reqVO.getBlindStatus())
                .eqIfPresent(ClerkDO::getWorkerClerkLevelId, reqVO.getWorkerClerkLevelId())
                .eqIfPresent(ClerkDO::getLevel, reqVO.getLevel())
                .eqIfPresent(ClerkDO::getHot, reqVO.getHot())
                .eqIfPresent(ClerkDO::getHome, reqVO.getHome())
                .eqIfPresent(ClerkDO::getSkillNum, reqVO.getSkillNum())
                .betweenIfPresent(ClerkDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ClerkDO::getId));
    }

    default PageResult<ClerkDO> selectPage(AppClerkPageReqVO reqVO) {
        if(StringUtils.isNotBlank(reqVO.getKeyword())){
            return selectPage(reqVO, new LambdaQueryWrapperX<ClerkDO>()
                    .eq(ClerkDO::getStatus, true)
                    .and((e)->{
                        e.or().like(ClerkDO::getNickname, reqVO.getKeyword());
                        e.or().like(ClerkDO::getIntro, reqVO.getKeyword());
                        e.or().like(ClerkDO::getCity, reqVO.getKeyword());
                    })
                    .orderByDesc(ClerkDO::getLevel, ClerkDO::getOrderNum, ClerkDO::getId));
        }
        return selectPage(reqVO, new LambdaQueryWrapperX<ClerkDO>()
                .likeIfPresent(ClerkDO::getNickname, reqVO.getNickname())
                .likeIfPresent(ClerkDO::getCategoryIdArray, reqVO.getCategoryId())
                .eqIfPresent(ClerkDO::getAvatar, reqVO.getAvatar())
                .eqIfPresent(ClerkDO::getSex, reqVO.getSex())
                .eqIfPresent(ClerkDO::getAge, reqVO.getAge())
                .eqIfPresent(ClerkDO::getWeixin, reqVO.getWeixin())
                .eqIfPresent(ClerkDO::getMobile, reqVO.getMobile())
                .eqIfPresent(ClerkDO::getCity, reqVO.getCity())
                .eqIfPresent(ClerkDO::getExperience, reqVO.getExperience())
                .eqIfPresent(ClerkDO::getSound, reqVO.getSound())
                .eqIfPresent(ClerkDO::getSoundType, reqVO.getSoundType())
                .betweenIfPresent(ClerkDO::getSoundTime, reqVO.getSoundTime())
                .eqIfPresent(ClerkDO::getAlbums, reqVO.getAlbums())
                .eqIfPresent(ClerkDO::getIntro, reqVO.getIntro())
                .betweenIfPresent(ClerkDO::getOnlineTime, reqVO.getOnlineTime())
                .eqIfPresent(ClerkDO::getOnlineStatus, reqVO.getOnlineStatus())
                .eqIfPresent(ClerkDO::getCatIds, reqVO.getCatIds())
                .eqIfPresent(ClerkDO::getCatList, reqVO.getCatList())
                .eqIfPresent(ClerkDO::getGameIds, reqVO.getGameIds())
                .eqIfPresent(ClerkDO::getGameList, reqVO.getGameList())
                .eqIfPresent(ClerkDO::getGoodsIds, reqVO.getGoodsIds())
                .eqIfPresent(ClerkDO::getFans, reqVO.getFans())
                .eqIfPresent(ClerkDO::getOrderNum, reqVO.getOrderNum())
                .eqIfPresent(ClerkDO::getBlindStatus, reqVO.getBlindStatus())
                .eqIfPresent(ClerkDO::getWorkerClerkLevelId, reqVO.getWorkerClerkLevelId())
                .eqIfPresent(ClerkDO::getLevel, reqVO.getLevel())
                .eqIfPresent(ClerkDO::getHot, reqVO.getHot())
                .eqIfPresent(ClerkDO::getHome, reqVO.getHome())
                .eqIfPresent(ClerkDO::getSkillNum, reqVO.getSkillNum())
                .eq(ClerkDO::getStatus, true)
                .betweenIfPresent(ClerkDO::getCreateTime, reqVO.getCreateTime())
                .orderByDesc(ClerkDO::getLevel, ClerkDO::getOrderNum, ClerkDO::getId));
    }

    default List<ClerkDO> getClerkHotList(){
        return selectList(new QueryWrapperX<ClerkDO>()
                .eq("status", true)
                .eq("hot", true).limitN(20));
    }

    default List<ClerkDO> getClerkNewList(){
        return selectList(new QueryWrapperX<ClerkDO>()
                .eq("status", true)
                .orderByDesc("create_time").limitN(20));
    }

    default List<ClerkDO> getClerkListByUserId(Long userId){
        return selectList(new LambdaQueryWrapperX<ClerkDO>()
                .eqIfPresent(ClerkDO::getUserId, userId)
                .eq(ClerkDO::getStatus, true));
    }

    default ClerkDO getClerkByOrder(Long userId, String sex){
        return selectOne(new LambdaQueryWrapperX<ClerkDO>()
                .eqIfPresent(ClerkDO::getUserId, userId)
                .eqIfPresent(ClerkDO::getSex, sex)
                .eq(ClerkDO::getStatus, true)
                .last("LIMIT 1"));
    }

    default List<ClerkDO> getWeekTopList(String sex){
        return selectList(new LambdaQueryWrapperX<ClerkDO>()
                .select(ClerkDO::getId,ClerkDO::getAvatar,ClerkDO::getNickname)
                .eq(ClerkDO::getSex, sex)
                .eq(ClerkDO::getStatus, true)
                .gt(ClerkDO::getBrokerageWeekNum, 0)
                .orderByDesc(ClerkDO::getBrokerageWeekNum).last("LIMIT 3"));
    }

    default ClerkDO getMainClerk(Long userId){
        ClerkDO clerkDO = selectOne(new LambdaQueryWrapperX<ClerkDO>()
                .eq(ClerkDO::getUserId, userId)
                .eq(ClerkDO::getStatus, true)
                .eq(ClerkDO::getMain, true).last("LIMIT 1"));
        if(clerkDO == null){
            clerkDO = selectOne(new LambdaQueryWrapperX<ClerkDO>()
                    .eq(ClerkDO::getUserId, userId)
                    .eq(ClerkDO::getStatus, true).last("LIMIT 1"));
        }
        return clerkDO;
    }

    default void updateFansCount(Long id, Integer fansNum){
        update(new LambdaUpdateWrapper<ClerkDO>()
                .set(ClerkDO::getFans, fansNum)
                .eq(ClerkDO::getId, id));
    }

    default List<ClerkDO> getClerkList(Long userId){
        return selectList(new LambdaQueryWrapperX<ClerkDO>()
                .eq(ClerkDO::getUserId, userId)
                .eq(ClerkDO::getStatus, true)
                .orderByDesc(ClerkDO::getCreateTime));
    }

    default ClerkDO getClerk(Long userId, Long id){
        return selectOne(new LambdaQueryWrapperX<ClerkDO>()
                .eq(ClerkDO::getUserId, userId)
                .eq(ClerkDO::getId, id)
                .eq(ClerkDO::getStatus, true));
    }

    default List<ClerkDO> getNewClerkList(){
        return selectList(new LambdaQueryWrapperX<ClerkDO>()
                .eq(ClerkDO::getBlindStatus, true)
                .eq(ClerkDO::getStatus, true)
                .orderByDesc(ClerkDO::getCreateTime).last("LIMIT 100"));
    }
}